package com.ruoyi.framework.config;

import org.springframework.context.annotation.Configuration;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

/**
 * @Description: shardingSphere分表配置
 * @Author WangHan
 * @Date 2024/9/19 15:28
 * @Version 1.0
 */
@Configuration
public class ShardingConfig {

    /** 配置文件路径 */
    private static final String CONFIG_FILE = "sharding.yaml";

    /**
     * 获取数据源配置
     */
    public static byte[] getShardingYAMLFile() throws IOException {
        InputStream inputStream = Objects.requireNonNull(
                ShardingConfig.class.getClassLoader().getResourceAsStream(CONFIG_FILE),
                String.format("Resource `%s` is not found in the classpath.", CONFIG_FILE)
        );
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        int nRead;
        byte[] data = new byte[16384]; // 创建一个合适的缓冲区大小

        // 读取InputStream到ByteArrayOutputStream
        while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }

        // 将ByteArrayOutputStream转换成byte数组
        buffer.flush();
        return buffer.toByteArray();
    }
}
